package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @Override
    public OverviewVo getOverview() {
        //查询redis缓存中的数据
        log.info("查询线索和商机概览数据");
        Object object = redisTemplate.opsForValue().get("OVERVIEW_DATA");

        if(object != null){//缓存中有数据
            log.info("从redis缓存中获取数据");
            return (OverviewVo) object;
        }
        //缓存中没数据,查询数据库
        //获取线索相应的数据
        OverviewVo clueOverview = clueMapper.getClueOverview();
        //获取商机相应的数据
        OverviewVo businessOverview = businessMapper.getBusinessOverview();

        //封装 OverviewVo
        BeanUtils.copyProperties( businessOverview,clueOverview, "clueTotal", "clueWaitAllot", "clueWaitFollow", "clueFollowing", "clueFalse", "clueConvertBusiness");

        log.info("将数据保存到redis缓存中");
        redisTemplate.opsForValue().set("OVERVIEW_DATA",clueOverview,1, TimeUnit.MINUTES);
        return clueOverview;

    }
}
